// packageD/quickPay/member_choose_bank/member_choose_bank.js
let app = getApp();
var yz_pay = require("../../../mycomponent/yz_pay/yz_pay");
Page({
  behaviors: [yz_pay],
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    codetext: "获取短信验证码",
    start1: false,
    order_pay_id: "",
    bank: [],
    show1: false,
    arr: [],
    amount: "",
    pay_sn: "",
    bank_name: "",
    code: "",
    order_pay_sn: "",
    payer_phone: "",
    pay_type: "",
    card_no: "",
    status: '',
    troundSn:'',
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData();
    this.setData({
      order_pay_id: options.order_pay_id,
    });
    if(options.status) {
      this.setData({
        status: options.status,
      });
    }
    if(options.status == 4) {
      this.setData({
        troundSn: options.order_pay_id
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    this.getType();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  initData() {
    this.setData({
      bank: [],
      show1: false,
      arr: [],
      amount: "",
      pay_sn: "",
      bank_name: "",
      code: "",
      order_pay_sn: "",
      payer_phone: "",
      pay_type: "",
      card_no: "",
    });
  },
  toPay(e) {
    let that = this;
    let item = e.currentTarget.dataset.item;
    if (item.status == 0 && this.data.pay_type == 2) {
      wx.navigateTo({
        url:
          "/packageD/quickPay/bind_bank/bind_bank?card_id=" +
          item.id +
          "&order_pay_id=" +
          that.data.order_pay_id + '&status='+that.data.status,
      });
    } else {
      this.setData({
        show1: true,
        card_no: item.card_no,
      });
      this.quick_pay();
      this.getInfo(item.id);
    }
  },
  deleteBank(e) {
    let item = e.currentTarget.dataset.item;
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.converge_pay.frontend.controllers.quick-pay-bank-card.del"
    );
    app._getNetWork({
      url: urlStr,
      data: { card_id: item.id },
      showToastIn: false,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
          that.setData({
            show1: false,
          });
          that.getBank();
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  toAdd() {
    let that = this;
    wx.navigateTo({
      url:
        "/packageD/quickPay/add_bank_first/add_bank_first?order_pay_id=" +
        that.data.order_pay_id + '&status=' + this.data.status,
    });
  },
  getBank() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.converge_pay.frontend.controllers.quick-pay-bank-card.index"
    );
    app._getNetWork({
      url: urlStr,
      showToastIn: false,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            bank: res.data,
          });
          if (that.data.pay_type == 2) {
            that.setData({
              arr: that.data.bank.filter((item) => {
                return item.status == 1;
              }),
            });
            if (that.data.arr.length == 1) {
              that.setData({
                show1: true,
                card_no: that.data.arr[0].card_no,
              });
              that.quick_pay();
              that.getInfo(that.data.arr[0].id);
            }
          } else if (that.data.pay_type == 1) {
            that.setData({
              arr: that.data.bank,
            });
            if (that.data.arr.length == 1) {
              that.setData({
                show1: true,
                card_no: that.data.arr[0].card_no,
              });
              that.quick_pay();
              that.getInfo(that.data.arr[0].id);
            }
          }
        } else {
          that.setData({
            bank: res.data.data,
          });
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  numInp(e) {
    let val = e.detail.value;
    this.setData({
      code: val,
    });
  },
  getType() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.converge_pay.frontend.controllers.quick-pay-bank-card.get-pay-type"
    );
    app._getNetWork({
      url: urlStr,
      showToastIn: false,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            pay_type: res.data.pay_type,
          });
          that.getBank();
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  quick_pay() {
    let that = this;
    let isurl = '';
    let json = {};
    if (this.data.status&&this.data.status==2) {
      isurl = app.getNetAddresss(
        'plugin.converge_pay.frontend.controllers.quick-pay.show-recharge'
      );
      json = {
        ordersn: that.data.order_pay_id
      };
    }else if(this.data.status&&this.data.status==3){
      isurl = app.getNetAddresss(
        'plugin.converge_pay.frontend.controllers.quick-pay.love-recharge'
      );
      json = {
        ordersn: that.data.order_pay_id
      };
    }else if(this.data.status&&this.data.status==4){
      isurl = app.getNetAddresss(
        'plugin.converge_pay.frontend.controllers.quick-pay.travelAround'
      );
      json = {
        ordersn: that.data.order_pay_id
      };
    } else if( this.data.status && this.data.status == 5){
      isurl = app.getNetAddresss(
        'plugin.converge_pay.frontend.controllers.quick-pay.pool-recharge'
      );
      json = {
        ordersn: that.data.order_pay_id
      };
    } else {
      isurl = app.getNetAddresss(
        'plugin.converge_pay.frontend.controllers.quick-pay.pay'
      );
      json = { order_pay_id: that.data.order_pay_id };
    }
    app._getNetWork({
      url: isurl,
      data: json,
      success: (res) => {
        if (res.data.result == 1) {
          that.setData({
            amount: res.data.data.order_pay.amount,
            pay_sn: res.data.data.order_pay.pay_sn,
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            duration: 1000,
            icon: "none",
          });
        }
      },
      fail: (err) => {
        console.log(err);
      },
    });
  },
  sendCode(e) {
    if (this.data.start1) {
      return false;
    }
    let time = 60;
    let that = this;
    that.setData({
      start1: true,
      codetext: "(" + time + ")秒后重新获取",
    });
    let set = setInterval(function () {
      that.setData({
        codetext: "(" + --time + ")秒后重新获取",
      });
    }, 1000);
    setTimeout(function () {
      that.setData({
        codetext: "获取短信验证码",
        start1: false,
      });
      clearInterval(set);
    }, 60000);
    that.verificationCode(set);
  },
  closePay() {
    this.setData({
      show1: false,
    });
  },
  // 获取验证码
  verificationCode(set) {
    let that = this;
    let urlStr = app.getNetAddresss("order.merge-pay.conberge-quick-pay");
    let json = {
      order_pay_id: that.data.order_pay_id,
      card_no: that.data.card_no,
    };
    if (this.data.status&&this.data.status==2) {
      urlStr = app.getNetAddresss(
        'plugin.converge_pay.frontend.controllers.quick-pay.recharge'
      );
      json = {
        ordersn: that.data.order_pay_id,
        card_no: that.data.card_no,
      };
    }else if(this.data.status&&this.data.status==3){
      urlStr = app.getNetAddresss(
        'plugin.converge_pay.frontend.controllers.quick-pay.love'
      );
      json = {
        ordersn: that.data.order_pay_id,
        card_no: that.data.card_no,
      };
    }else if(this.data.status&&this.data.status==4){
      urlStr = app.getNetAddresss(
        'plugin.converge_pay.frontend.controllers.quick-pay.travelAroundPay'
      );
      json = {
        ordersn: that.data.order_pay_id,
        card_no: that.data.card_no,
      };
    }
    //发送获取验证码的请求//旧的请求：member.register.sendCodeV2
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            order_pay_sn: res.data.order_pay_sn,
          });
          wx.showToast({
            icon: "none",
            title: "已发送",
            duration: 1500,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  pay(set) {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.converge_pay.frontend.controllers.quick-pay.sms-pay"
    );
    //发送获取验证码的请求//旧的请求：member.register.sendCodeV2
    app._postNetWork({
      url: urlStr,
      data: {
        sms_code: this.data.code,
        order_pay_sn: this.data.pay_sn,
        order_pay_id: this.data.order_pay_id
      },
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
          that.setData({
            show1: false,
          });
          this.toRedirect();
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getInfo(id) {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.converge_pay.frontend.controllers.quick-pay-bank-card.get-card"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        card_id: id,
      },
      showToastIn: false,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            payer_phone: res.data.payer_phone,
            bank_name: res.data.bank_name,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
});
